﻿using System;
using System.IO;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Text.RegularExpressions;
using WMSABL;
using WMSABL.SY;
using WMSAML.SY;
using ElectronicWMSA.Comm.ClassFiles;
using SYDZ.WMS.HelpClass;
using WMSAML.Warehouse.Model;



namespace ElectronicWMSA.Warehouse
{
    public partial class List_PositionFrm : Form
    {
        #region
        private string strWhere = "";
        PublicDataSet bll_CommonPub = new PublicDataSet();
        public List_PositionFrm()
        {
            InitializeComponent();
            this.strWhere = "";
            this.DBindCombox();
            this.comboBoxPositionStauts.SelectedIndex = 0;
            this.pageControlU.PageCurrent = 1;
            this.pageControlU.PageSize = 30;
            this.pageControlU.Bind();
        }
        #endregion

        #region
        public void DBindCombox()
        {
            if (true)
            {
                DataSet ds = new DataSet();
                ds = bll_CommonPub.GetDataSet("PD_Warehouse", "");
                DataTable dt = ds.Tables[0];
                DataRow dr = dt.NewRow();
                dr["NID"] = "0";
                dr["WarehouseName"] = "--请选择--";
                dt.Rows.InsertAt(dr, 0);
                this.comboBoxWareHouse.DataSource = dt;
                this.comboBoxWareHouse.ValueMember = "NID";
                this.comboBoxWareHouse.DisplayMember = "WarehouseName";

                this.comboBoxWareHouse2.DataSource = dt;
                this.comboBoxWareHouse2.ValueMember = "NID";
                this.comboBoxWareHouse2.DisplayMember = "WarehouseName";
            }
            if (true)
            {
                DataSet ds = new DataSet();
                ds = bll_CommonPub.GetDataSet("PD_Area", "");
                DataTable dt = ds.Tables[0];
                DataRow dr = dt.NewRow();
                dr["NID"] = "0";
                dr["AreaNO"] = "--请选择--";
                dt.Rows.InsertAt(dr, 0);
                this.comboBoxArea.DataSource = dt;
                this.comboBoxArea.ValueMember = "NID";
                this.comboBoxArea.DisplayMember = "AreaNO";

                this.comboBoxArea2.DataSource = dt;
                this.comboBoxArea2.ValueMember = "NID";
                this.comboBoxArea2.DisplayMember = "AreaNO";
            }
        }
        #endregion

        #region 数据
        private int DBindU()
        {
            ElectronicWMSA.Comm.Forms.MsControls.PageData pageData = new ElectronicWMSA.Comm.Forms.MsControls.PageData();
            pageData.TableName = "T_Position";
            pageData.PrimaryKey = "Warehouse_id";
            pageData.OrderStr = "Warehouse_id";
            pageData.PageIndex = this.pageControlU.PageCurrent;
            pageData.PageSize = this.pageControlU.PageSize;
            pageData.QueryCondition = strWhere;
            pageData.QueryFieldName = "*";

            this.pageControlU.bindingSource.DataSource = pageData.QueryDataTable().Tables[0];
            this.pageControlU.bindingNavigator.BindingSource = pageControlU.bindingSource;
            this.dataGridViewUserList.AutoGenerateColumns = false;
            this.dataGridViewUserList.DataSource = this.pageControlU.bindingSource;
            this.dataGridViewUserList.ClearSelection();
            this.dataGridViewUserList.CurrentCell = null;
            return pageData.TotalCount;
        }
        /// <summary>
        /// 控件事件
        /// </summary>
        private int pageControlU_EventPaging(ElectronicWMSA.Comm.Forms.MsControls.EventPagingArg e)
        {
            return DBindU();
        }
        #endregion

        #region 查询
        private void btnUserSearch_Click(object sender, EventArgs e)
        {
            if (!SystemVariable.VaildationPermission("查询")) return;
            strWhere = " BK02=1 ";
            if (this.comboBoxWareHouse.Text.Trim() != "" && this.comboBoxWareHouse.Text.Trim() != "--请选择--")
                this.strWhere += " AND Subwarehouse_no='" + this.comboBoxWareHouse.Text.Trim() + "' ";
            if (this.comboBoxArea.Text.Trim() != "--请选择--" && this.comboBoxArea.Text.Trim() != "")
                this.strWhere += " AND Region_no='" + this.comboBoxArea.Text.Trim() + "' ";
            if (this.comBoxPosition.Text.Trim() != "--请选择--" && this.comBoxPosition.Text.Trim() != "")
                this.strWhere += " AND Warehouse_no LIKE '%" + this.comBoxPosition.Text.Trim() + "%' ";
            this.pageControlU.Bind();//
        }
        #endregion

        #region  新增、保存
        WMSAML.Warehouse.Model.T_Position tpmode = new WMSAML.Warehouse.Model.T_Position();
        WMSAML.Warehouse.BLL.T_Position tpbll = new WMSAML.Warehouse.BLL.T_Position();
        private void btnUSave_Click(object sender, EventArgs e)
        {
            if (comboBoxWareHouse2.Text == "--请选择--")
            {
                MessageBox.Show("请选择仓库");
                return;
            }
            if (comboBoxArea2.Text == "--请选择--")
            {
                MessageBox.Show("请选择区域");
                return;
            }
            if (string.IsNullOrEmpty(txtName.Text) || string.IsNullOrEmpty(txtCode.Text))
            {
                MessageBox.Show("请填写库位");
                return;
            }
            tpmode.Subwarehouse_id = Convert.ToInt32(comboBoxWareHouse2.SelectedValue);
            tpmode.Subwarehouse_no = "8000";//comboBoxWareHouse2.Text;
            tpmode.Region_id = Convert.ToInt32(comboBoxArea2.SelectedValue);
            tpmode.Region_no = comboBoxArea2.Text;
            tpmode.Warehouse_no = txtName.Text;
            tpmode.Warehouse_name = txtCode.Text;
            tpmode.Warehouse_width = 0;
            tpmode.Warehouse_height = 0;
            tpmode.Warehouse_highness = 0;
            tpmode.Max_inventory = 0;
            tpmode.Normal_stock = 0;
            tpmode.Warehouse_status = 0;
            tpmode.Mark = txtEmail.Text;
            tpmode.Create_time = DateTime.Now.ToString();
            tpmode.Update_time = DateTime.Now.ToString();
            tpmode.Update_user_id = "1";
            tpmode.BK01 = "未分配";
            tpmode.BK02 = "1";
            if (tpbll.Add(tpmode) > 0)
            {
                MessageBox.Show("添加成功");

            }
            else
            {
                MessageBox.Show("添加失败");
            }
        }
        private void btnNewUser_Click(object sender, EventArgs e)
        {
            if (!SystemVariable.VaildationPermission("新增")) return;
            this.btnUSave.Text = "添加";
            this.tabAnalysisList.SelectedIndex = 1;
        }

        #endregion

        #region 序号
        private void dataGridViewUserList_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
        {
            SolidBrush b = new SolidBrush(dataGridViewUserList.RowHeadersDefaultCellStyle.ForeColor);
            e.Graphics.DrawString((e.RowIndex + 1).ToString(System.Globalization.CultureInfo.CurrentUICulture), dataGridViewUserList.DefaultCellStyle.Font, b, e.RowBounds.Location.X + 20, e.RowBounds.Location.Y + 4);
        }
        #endregion

        #region 退出
        private void linkLabelClose_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            Main._Main.CloseTabPages();
        }

        private void linkLabellinkLabelCloseAll_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            Main._Main.CloseAllTabPages();
        }

        private void linkLabelCloseElse_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            Main._Main.CloseTabPageElse();
        }
        #endregion

        #region 返回列表
        private void btnRetunList_Click(object sender, EventArgs e)
        {
            this.tabAnalysisList.SelectedIndex = 0;
        }
        #endregion
        DataTable dt = new DataTable();
        INPortExcel inp = new INPortExcel();
        WMSAML.Warehouse.BLL.T_Position BLLTP = new WMSAML.Warehouse.BLL.T_Position();
        WMSAML.Warehouse.Model.T_Position ModelTP = new WMSAML.Warehouse.Model.T_Position();
        private void button1_Click(object sender, EventArgs e)
        {
            OpenFileDialog fd = new OpenFileDialog();
            fd.Filter = "Microsoft Excel files(*.xls,*.xlsx)|*.xls;*.xlsx|CSV文件|*.csv";
            if (DialogResult.OK == fd.ShowDialog(this))
            {
                string filename = fd.FileName;
                int x = filename.LastIndexOf("\\");


                if (inp.IsFileInUse(filename))
                {
                    MessageBox.Show("文件已打开或被其他程序占用，请检查并关闭占用程序！", "提示");
                    return;

                }
                      int num = 0;
                dt = inp.GetExcelDatatable(filename, true);
                foreach (DataRow dr in dt.Rows)
                {
                    ModelTP.Subwarehouse_id = Convert.ToInt32(dr["仓库ID"].ToString());
                    ModelTP.Subwarehouse_no = dr["仓库"].ToString();
                    ModelTP.Region_id = Convert.ToInt32(dr["区域ID"].ToString());
                    ModelTP.Region_no = dr["区域"].ToString();
                    ModelTP.Warehouse_no = dr["仓位"].ToString();
                    ModelTP.Warehouse_name = dr["仓位"].ToString();
                    ModelTP.Warehouse_width = 0;
                    ModelTP.Warehouse_height = 0;
                    ModelTP.Warehouse_highness = 0;
                    ModelTP.Max_inventory = 0;
                    ModelTP.Normal_stock = 0;
                    ModelTP.Warehouse_status = 0;
                    ModelTP.Create_time = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
                    ModelTP.Update_time = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
                    ModelTP.Update_user_id = "1";
                    ModelTP.BK01 = "未分配";
                    ModelTP.BK02 = "1";
              
                    try
                    {
                        BLLTP.Add(ModelTP);
                        num++;
                    }
                    catch {
                        num = 0;
                        break;
                    }

                }
                if (num > 0) { MessageBox.Show("成功"); }
            }
        }

        private void dataGridViewUserList_CellClick(object sender, DataGridViewCellEventArgs e)
        {

        }

    }
}
